package org.top10.executor;

import it.sauronsoftware.cron4j.SchedulingPattern;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.top10.dao.DBServer;
import org.top10.dealor.TaskCreator;


import sun.util.logging.resources.logging;

//入口类
public class NdcDaemonImpl implements NdcDaemon {
	
	private final static Logger log = Logger.getLogger(NdcDaemonImpl.class
			.getName());

	private TaskTriggerScheduler jobTriggerScheduler;

	public NdcDaemonImpl(){
		jobTriggerScheduler=new Cron4jTaskTriggerScheduler();
	}

	private void loadTaskData() {
//		if(TaskCreator.tasks.size()==0){
//			System.exit(-1);
//		}
//		System.out.println("task size is="+TaskCreator.tasks.size());
//		for (CollectTask task : TaskCreator.tasks) {
//			if (!SchedulingPattern.validate(task.getCollectTime())) {
//				log.error("crontabExpression is invalide! task name is"+task.getName());
//				continue;
//			}
//			CollectTask triggerJob =TaskCreator.tasks.get(0);
//			jobTriggerScheduler.addTriggerTask(triggerJob.getCollectTime(), triggerJob);//把job放进任务调度表，时间到了会执行triggerJob的run方法
//			log.info("Start to add device!!!");
//			
//		}
	}


	public void execute() {
		jobTriggerScheduler=new Cron4jTaskTriggerScheduler();
		loadTaskData();
		jobTriggerScheduler.start();
	}

	public static void main(String[] args) {
		log
				.info("************************ SSDRDaemon Start Now ****************************");
		try {
			ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext*.xml");
			context.getBean("dbServer");
			NdcDaemon d=new NdcDaemonImpl();
			System.out.println("running...");
			d.execute();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
		while (true) {
			try {
				Thread.sleep(60000); //60 seconds
			} catch (InterruptedException e) {
				e.printStackTrace();
				log.warn("get InterruptedException exception:"
						+ e.getStackTrace());
			}
		}
	}


}